<?php

/**
 * An array key getter
 *
 * Use this when you have one-level array,
 * ie: $arr = array('key1' => 'value1', 'key2' => 'value2');
 *
 * @param array  $array   array from which you want to get data
 * @param string $key     array key you want to extract
 * @param mixed  $default fallback value in case of non-existent key
 *
 * @return
 *   if the passed key of the array exists, return the corresponding value,
 *   else return the default value(if passed); as last resort return null.
 */
function akh_get(array $array, $key, $default = null) {
    return (isset($array[$key])) ? $array[$key] : $default;
}

/**
 * A recursive array key getter
 *
 * Use this when you have multiple-levels array,
 * ie: $arr = array('key1' => array('key3' => array('key4' => 'value4')),
 *                  'key2' => 'value2');
 *
 * @param array $array   array from which you want to get data
 * @param array $key     array containing index path of the key you want
 *                       to extract(ie: array('a', 'b', 'c')
 *                       will point to $array['a']['b']['c'])
 * @param mixed $default fallback value in case of non-existent key
 *
 * @return
 *   if the passed key of the array exists, return the corresponding value,
 *   else return the default value(if passed); as last resort return null.
 */
function akh_get_recursive(array $array, array $key, $default = null) {
    $inner_key = array_shift($key);
    return isset($array[$inner_key]) ?
        (empty($key) ?
            $array[$inner_key] :
            akh_get_recursive($array[$inner_key], $key, $default)) :
        $default;
}
